package edu.api.set_api;

import java.util.Iterator;
import java.util.TreeSet;
import java.util.function.Consumer;

public class Demo2 {

    public static void main(String[] args) {
        test2();
    }

    /**
     * 当TreeSet保存的数据没有实现Comparable接口时，会报错，此时需要手动实现此接口
     */
    public static void test2(){
        Product pd1 = new Product(4, "木糖醇", 10);
        Product pd2 = new Product(5, "洗发水", 12);
        Product pd3 = new Product(3, "热水壶", 49);
        Product pd4 = new Product(3, "热水壶", 49);

        TreeSet<Product> products = new TreeSet<>();
        products.add(pd1);
        products.add(pd2);
        products.add(pd3);
        products.add(pd4);
        System.out.println("products = " + products);
    }


    public static void testFor(){
        TreeSet<Double> scores = new TreeSet<>();
        scores.add(89.5);
        scores.add(69.2);
        scores.add(79.0);
        scores.add(90.2);

        for (Double score : scores) {

        }

        Iterator<Double> iterator = scores.iterator();
        while (iterator.hasNext()) {
            System.out.println("iterator.next() = " + iterator.next());
        }

        scores.forEach(new Consumer<Double>() {
            @Override
            public void accept(Double aDouble) {
                System.out.println("aDouble = " + aDouble);
            }
        });


    }

    public static void test1(){
        TreeSet<Double> scores = new TreeSet<>();
        scores.add(89.5);
        scores.add(69.2);
        scores.add(79.0);
        scores.add(90.2);
        System.out.println("scores = " + scores);

        Double first = scores.first();
        System.out.println("first = " + first);
        Double floor = scores.floor(80.5);
        System.out.println("floor = " + floor);
        Double higher = scores.higher(80.5);
        System.out.println("higher = " + higher);
        //获取最后一个元素并删除
        Double last = scores.pollLast();
        System.out.println("last = " + last);
        System.out.println("scores = " + scores);

        Double ceiling = scores.ceiling(70.2);
        System.out.println("ceiling = " + ceiling);
    }

}
